home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 2002 November / SGI IRIX Base Documentation 2002 November.iso / usr / share / catman / u_man / cat1 / printf.z / printf
Encoding:
Text File  |  2002-10-03  |  16.3 KB  |  331 lines

  1.  
  2.  
  3.  
  4. pppprrrriiiinnnnttttffff((((1111))))                                                            pppprrrriiiinnnnttttffff((((1111))))
  5.  
  6.  
  7.  
  8. NNNNAAAAMMMMEEEE
  9.      _pppp_rrrr_iiii_nnnn_tttt_ffff - print formatted output
  10.  
  11. SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
  12.      _pppp_rrrr_iiii_nnnn_tttt_ffff _f_o_r_m_a_t [_a_r_g . . .]
  13.  
  14. DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  15.      The _pppp_rrrr_iiii_nnnn_tttt_ffff command converts, formats, and prints its _a_r_gs under control
  16.      of the _f_o_r_m_a_t.  It fully supports conversion specifications for strings
  17.      (_%%%%_ssss descriptor); however, the results are undefined for the other
  18.      conversion specifications not specifically mentioned below.
  19.  
  20.      _f_o_r_m_a_t    a character string that contains three types of objects:  1)
  21.                plain characters, which are simply copied to the output stream;
  22.                2) conversion specifications, each of which results in fetching
  23.                zero or more _a_r_gs; and 3) C-language escape sequences, which
  24.                are translated into the corresponding characters.
  25.  
  26.      _a_r_g       string(s) to be printed under the control of _f_o_r_m_a_t.  The
  27.                results are undefined if there are insufficient _a_r_gs for the
  28.                format.
  29.  
  30.      Each conversion specification is introduced by the character _%%%%.  After
  31.      the _%%%%, the following appear in sequence:
  32.  
  33.           An optional field, consisting of a decimal digit string followed by
  34.           a _$$$$, specifying the next _a_r_g to be converted.  If this field is not
  35.           provided, the _a_r_g following the last _a_r_g converted is used.
  36.  
  37.           An optional decimal digit string specifying a minimum _f_i_e_l_d _w_i_d_t_h.
  38.           If the converted value has fewer characters than the field width, it
  39.           is padded on the left (or right, if the left-adjustment flag `_----' has
  40.           been given) to the field width.  The padding is with blanks unless
  41.           the field width digit string starts with a zero, in which case the
  42.           padding is with zeros.
  43.  
  44.           An optional _p_r_e_c_i_s_i_o_n that gives the maximum number of characters to
  45.           be printed from a string in _%%%%_ssss conversion.  The precision takes the
  46.           form of a period (_....)  followed by a decimal digit string; a null
  47.           digit string is treated as zero (nothing is printed).  Padding
  48.           specified by the precision overrides the padding specified by the
  49.           field width.  That is, if _p_r_e_c_i_s_i_o_n is specified, its value is used
  50.           to control the number of characters printed.
  51.  
  52.           A field width or precision or both may be indicated by an asterisk
  53.           (_****) instead of a digit string.  In this case, an integer _a_r_g
  54.           supplies the field width or precision.  The _a_r_g that is actually
  55.           converted is not fetched until the conversion letter is seen, so the
  56.           _a_r_gs specifying field width or precision must appear _b_e_f_o_r_e the _a_r_g
  57.           (if any) to be converted.  A negative field width argument is taken
  58.           as a _````_----_'''' (left-adjustment) flag followed by a positive field width.
  59.           If the precision argument is negative, it is changed to zero
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. pppprrrriiiinnnnttttffff((((1111))))                                                            pppprrrriiiinnnnttttffff((((1111))))
  71.  
  72.  
  73.  
  74.           (nothing is printed).  In no case does a non-existent or small field
  75.           width cause truncation of a field; if the result of a conversion is
  76.           wider than the field width, the field is simply expanded to contain
  77.           the conversion result.
  78.  
  79.      The conversion characters and their meanings are:
  80.  
  81.      _%%%%_eeee_,,,, _%%%%_EEEE_,,,, _%%%%_ffff_,,,, _%%%%_gggg _aaaa_nnnn_dddd _%%%%_GGGG
  82.           These conversion specifications are not supported.
  83.  
  84.      _%%%%_bbbb   The argument will be taken to be a string that may contain
  85.           backslash-escape sequences.
  86.  
  87.      _%%%%_ssss   The _a_r_g is taken to be a string and characters from the string are
  88.           printed until a null character (_\\\\_0000) is encountered or the number of
  89.           characters indicated by the precision specification is reached.  If
  90.           the precision is missing, it is taken to be infinite, so all
  91.           characters up to the first null character are printed.  A null value
  92.           for _a_r_g yields undefined results.
  93.  
  94.      _%%%%_%%%%   Print a _%%%%; no argument is converted.
  95.  
  96. EEEEXXXXTTTTEEEENNNNDDDDEEEEDDDD DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  97.      _p_r_i_n_t_f does not precede or follow output from the _%%%%_dddd or _%%%%_uuuu conversion
  98.      specifications with blank characters not specified by the format operand.
  99.      _p_r_i_n_t_f does not precede output from the _%%%%_oooo conversion specification with
  100.      zeros not specified by the format operand.
  101.  
  102.      The following backslash-escape sequences are supported:
  103.  
  104.      File Format Notation (_\\\\_\\\\_,,,, _\\\\_aaaa_,,,, _\\\\_bbbb_,,,, _\\\\_ffff_,,,, _\\\\_nnnn_,,,, _\\\\_rrrr_,,,, _\\\\_tttt_,,,, _\\\\_vvvv), which will be
  105.      converted to the characters they represent.
  106.  
  107.      _\\\\_0000_dddd_dddd_dddd,
  108.  
  109.      where ddd is a zero-, one-, two- or three-digit octal number that will be
  110.      converted to a byte with the numeric value specified by the octal
  111.      number_\\\\_cccc, which will not be written and will cause printf to ignore any
  112.      remaining characters in the string operand containing it, any remaining
  113.      string operands and any additional characters in the format operand.
  114.  
  115.      The interpretation of a backslash followed by any other sequence of
  116.      characters is unspecified.
  117.  
  118.      Bytes from the converted string will be written until the end of the
  119.      string or the number of bytes indicated by the precision specification is
  120.      reached.
  121.  
  122.      If the precision is omitted, it will be taken to be infinite, so all
  123.      bytes up to the end of the converted string will be written.  For each
  124.      specification that consumes an argument, the next argument operand will
  125.      be evaluated and converted to the appropriate type for the conversion as
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. pppprrrriiiinnnnttttffff((((1111))))                                                            pppprrrriiiinnnnttttffff((((1111))))
  137.  
  138.  
  139.  
  140.      specified below.
  141.  
  142.      The format operand will be reused as often as necessary to satisfy the
  143.      argument operands.
  144.  
  145.      Any extra _%%%%_cccc or _%%%%_ssss conversion specifications will be evaluated as if a
  146.      null string argument were supplied; other extra conversion specifications
  147.      will be evaluated as if a zero argument were supplied.
  148.  
  149.      If the format operand contains no conversion specifications and argument
  150.      operands are present, the results are unspecified.  If a character
  151.      sequence in the format operand begins with a _%%%% character, but does not
  152.      form a valid conversion specification, the behaviour is unspecified.The
  153.      argument operands will be treated as strings if the corresponding
  154.      conversion character is _%%%%_bbbb, _%%%%_cccc or _%%%%_ssss; otherwise, it will be evaluated as
  155.      a C constant, as described by the ISO C standard, with the following
  156.      extensions:
  157.  
  158.      A leading plus or minus sign will be allowed.
  159.  
  160.      If the leading character is a single- or double-quote, the value will be
  161.      the numeric value in the underlying codeset of the character following
  162.      the single- or double-quote.
  163.  
  164.      If an argument operand cannot be completely converted into an internal
  165.      value appropriate to the corresponding conversion specification, a
  166.      diagnostic message will be written to standard error and the utility will
  167.      not exit with a zero exit status, but will continue processing any
  168.      remaining operands and will write the value accumulated at the time the
  169.      error was detected to standard output.
  170.  
  171. EEEEXXXXIIIITTTT SSSSTTTTAAAATTTTUUUUSSSS
  172.      The following exit values are returned:
  173.  
  174.                 0   Successful completion.
  175.                >0   An error occurred.
  176.  
  177.      If an argument cannot be parsed correctly for the corresponding
  178.      conversion specification, the printf utility is required to report an
  179.      error.  Thus, overflow and extraneous characters at the end of an
  180.      argument being used for a numeric conversion are to be reported as
  181.      errors.  It is not considered an error if an argument operand is not
  182.      completely used for a _%%%%_cccc or _%%%%_ssss conversion or if a string operand's first
  183.      or second character is used to get the numeric value of a character.
  184.  
  185.      The printf utility is required to notify the user when conversion errors
  186.      are detected while producing numeric output; thus, the following results
  187.      would be expected with 32-bit twos-complement integers when %d is
  188.      specified as the format operand:
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. pppprrrriiiinnnnttttffff((((1111))))                                                            pppprrrriiiinnnnttttffff((((1111))))
  203.  
  204.  
  205.  
  206.      _D_i_a_g_n_o_s_t_i_c _O_u_t_p_u_t
  207.                printf: "5a" not completely converted
  208.                printf: "9999999999" arithmetic overflow
  209.                printf: "-9999999999" arithmetic overflow
  210.                printf: "ABC" expected numeric value
  211.  
  212.      Note that the value shown on standard output is what would be expected as
  213.      the return value from the function strtol.  A similar correspondence
  214.      exists between %u and strtoul.
  215.  
  216. EEEEXXXXAAAAMMMMPPPPLLLLEEEESSSS
  217.      The command
  218.  
  219.           _pppp_rrrr_iiii_nnnn_tttt_ffff _''''_%%%%_ssss _%%%%_ssss _%%%%_ssss_\\\\_nnnn_'''' _GGGG_oooo_oooo_dddd _MMMM_oooo_rrrr_nnnn_iiii_nnnn_gggg _WWWW_oooo_rrrr_llll_dddd
  220.  
  221.      results in the output:
  222.  
  223.           Good Morning World
  224.  
  225.      The following command produces the same output.
  226.  
  227.           _pppp_rrrr_iiii_nnnn_tttt_ffff _''''_%%%%_2222_$$$$_ssss _%%%%_ssss _%%%%_1111_$$$$_ssss_\\\\_nnnn_'''' _WWWW_oooo_rrrr_llll_dddd _GGGG_oooo_oooo_dddd _MMMM_oooo_rrrr_nnnn_iiii_nnnn_gggg
  228.  
  229.      Here is an example that prints the first 6 characters of _$$$$_PPPP_AAAA_TTTT_HHHH left-
  230.      adjusted in a 10-character field:
  231.  
  232.           _pppp_rrrr_iiii_nnnn_tttt_ffff _''''_FFFF_iiii_rrrr_ssss_tttt _6666 _cccc_hhhh_aaaa_rrrr_ssss _oooo_ffff _%%%%_ssss _aaaa_rrrr_eeee _%%%%_----_1111_0000_...._6666_ssss_...._0000 _$$$$_PPPP_AAAA_TTTT_HHHH _$$$$_PPPP_AAAA_TTTT_HHHH
  233.  
  234.      If _$$$$_PPPP_AAAA_TTTT_HHHH has the value _////_uuuu_ssss_rrrr_////_bbbb_iiii_nnnn_::::_////_uuuu_ssss_rrrr_////_llll_oooo_cccc_aaaa_llll_////_bbbb_iiii_nnnn, then the above command
  235.      would print the following output:
  236.  
  237.           _FFFF_iiii_rrrr_ssss_tttt _6666 _cccc_hhhh_aaaa_rrrr_ssss _oooo_ffff _////_uuuu_ssss_rrrr_////_bbbb_iiii_nnnn_::::_////_uuuu_ssss_rrrr_////_llll_oooo_cccc_aaaa_llll_////_bbbb_iiii_nnnn _aaaa_rrrr_eeee _////_uuuu_ssss_rrrr_////_bbbb    _....
  238.  
  239.      To alert the user and then print and read a series of prompts:
  240.  
  241.              _pppp_rrrr_iiii_nnnn_tttt_ffff _""""_\\\\_aaaa_PPPP_llll_eeee_aaaa_ssss_eeee _ffff_iiii_llll_llll _iiii_nnnn _tttt_hhhh_eeee _ffff_oooo_llll_llll_oooo_wwww_iiii_nnnn_gggg_:::: _\\\\_nnnn_NNNN_aaaa_mmmm_eeee_:::: _""""
  242.              _rrrr_eeee_aaaa_dddd _nnnn_aaaa_mmmm_eeee
  243.              _pppp_rrrr_iiii_nnnn_tttt_ffff _""""_PPPP_hhhh_oooo_nnnn_eeee _nnnn_uuuu_mmmm_bbbb_eeee_rrrr_:::: _""""
  244.              _rrrr_eeee_aaaa_dddd _pppp_hhhh_oooo_nnnn_eeee
  245.  
  246.      To read out a list of right and wrong answers from a file, calculate the
  247.      percentage correctly, and print them out.  The numbers are right-
  248.      justified and separated by a single tab character.  The percentage is
  249.      written to one decimal place of accuracy:
  250.  
  251.              _wwww_hhhh_iiii_llll_eeee _rrrr_eeee_aaaa_dddd _rrrr_iiii_gggg_hhhh_tttt _wwww_rrrr_oooo_nnnn_gggg _;;;; _dddd_oooo
  252.              _pppp_eeee_rrrr_cccc_eeee_nnnn_tttt_====_$$$$_((((_eeee_cccc_hhhh_oooo _""""_ssss_cccc_aaaa_llll_eeee_====_1111_;;;;_((((_$$$$_rrrr_iiii_gggg_hhhh_tttt_****_1111_0000_0000_))))_////_((((_$$$$_rrrr_iiii_gggg_hhhh_tttt_++++_$$$$_wwww_rrrr_oooo_nnnn_gggg_))))_"""" _|||| _bbbb_cccc_))))
  253.              _pppp_rrrr_iiii_nnnn_tttt_ffff _""""_%%%%_2222_dddd _rrrr_iiii_gggg_hhhh_tttt_%%%%_2222_dddd _wwww_rrrr_oooo_nnnn_gggg_((((_%%%%_ssss_%%%%_%%%%_))))_0000 _\\\\
  254.               _$$$$_rrrr_iiii_gggg_hhhh_tttt _$$$$_wwww_rrrr_oooo_nnnn_gggg _$$$$_pppp_eeee_rrrr_cccc_eeee_nnnn_tttt
  255.              _dddd_oooo_nnnn_eeee _<<<< _dddd_aaaa_tttt_aaaa_bbbb_aaaa_ssss_eeee______ffff_iiii_llll_eeee
  256.  
  257.  
  258.  
  259.  
  260.  
  261.                                                                         PPPPaaaaggggeeee 4444
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. pppprrrriiiinnnnttttffff((((1111))))                                                            pppprrrriiiinnnnttttffff((((1111))))
  269.  
  270.  
  271.  
  272.      The command:
  273.  
  274.              _pppp_rrrr_iiii_nnnn_tttt_ffff _""""_%%%%_5555_dddd_%%%%_4444_dddd_\\\\_nnnn_"""" _1111 _2222_1111 _3333_2222_1111 _4444_3333_2222_1111 _5555_4444_3333_2222_1111
  275.  
  276.      produces:
  277.  
  278.               _1111   _2222_1111
  279.             _3333_2222_1111 _4444_3333_2222_1111
  280.           _5555_4444_3333_2222_1111    _0000
  281.  
  282.      Note that the format operand is used three times to print all of the
  283.      given strings and that a 0 was supplied by printf to satisfy the last %4d
  284.      conversion specification.
  285.  
  286. SSSSEEEEEEEE AAAALLLLSSSSOOOO
  287.      _pppp_rrrr_iiii_nnnn_tttt_ffff(3S)
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.                                                                         PPPPaaaaggggeeee 5555
  328.  
  329.  
  330.  
  331.